<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8" />
		<title>用户列表 —个人中心</title>
		<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
		<link   rel="icon" href="${base}/favicon.ico" type="image/x-icon" />
		<link   rel="shortcut icon" href="${base}/favicon.ico" />
		<link href="${base}/js/common/bootstrap/3.3.5/css/bootstrap.min.css?" rel="stylesheet"/>
		<link href="${base}/css/common/base.css?" rel="stylesheet"/>
		<script  src="${base}/js/common/jquery/jquery1.8.3.min.js"></script>
		<script  src="${base}/js/common/layer/layer.js"></script>
		<script  src="${base}/js/common/bootstrap/3.3.5/js/bootstrap.min.js"></script>
		<script  src="${base}/js/shiro.demo.js"></script>
		<script >
			so.init(function(){
				//初始化全选。
				so.checkBoxInit('#checkAll','[check=box]');
				
				//全选
				so.id('deleteAll').on('click',function(){
					var checkeds = $('[check=box]:checked');
					if(!checkeds.length){
						return layer.msg('请选择要删除的选项。',so.default),!0;
					}
					var array = [];
					checkeds.each(function(){
						array.push(this.value);
					});
					return deleteById(array);
				});
				
			});
			
			<#--根据ID数组删除角色-->
			function deleteById(ids){
				var index = layer.confirm("确定这"+ ids.length +"个权限？",function(){
					var load = layer.load();
					$.post('${basePath}/user/deleteUserById.shtml',{ids:ids.join(',')},function(result){
						layer.close(load);
						if(result && result.status != 200){
							return layer.msg(result.message,so.default),!0;
						}else{
							layer.msg(result.resultMsg);
							setTimeout(function(){
								$('#formId').submit();
							},1000);
						}
					},'json');
					layer.close(index);
				});
			}
		</script>
	</head>
	<body data-target="#one" data-spy="scroll">
		<#import "../common/config/top.ftl" as _top/>
	    <#import "../common/config/left.ftl" as _left/>
		<@_top.top 2/>
		<div class="container" style="padding-bottom: 15px;min-height: 300px; margin-top: 40px;">
			<div class="row">
			<@_left.member 1/>
				<div class="col-md-10">
					<h2>用户列表</h2>
					<hr>
					<form method="post" action="" id="formId" class="form-inline">
						<div clss="well">
					      <div class="form-group">
					        <input type="text" class="form-control" style="width: 300px;" value="${findContent?default('')}" 
					        			name="findContent" id="findContent" placeholder="输入昵称 / 帐号">
					      </div>
					     <span class=""> <#--pull-right -->
				         	<button type="submit" class="btn btn-primary">查询</button>
				         	
				         		<button type="button" id="deleteAll" class="btn  btn-danger">Delete</button>
				         	
				         </span>    
				        </div>
					<hr>
					<table class="table table-bordered">
					<input type="hidden" id="selectUserId">
						<tr>
							<th><input type="checkbox" id="checkAll"/></th>
							<th>昵称</th>
							<th>Email/帐号</th>
							<th>性别</th>
		
							<th>角色</th>
							<th>操作</th>
						</tr>
						<#if userList?exists && userList?size gt 0 >
							<#list userList as it>
								<tr>
									<td><input value="${it.id}" check='box' type="checkbox" /></td>
									<td>${it.nickname?default('未设置')}</td>
									<td>${it.email?default('未设置')}</td>
									<td>${it.sex?default('未设置')}</td>
									
									<td><#list it.roles as role>
									         ${role.name}&nbsp;&nbsp;
									    </#list>
									</td>
									<td>
									
										
										<i class="glyphicon glyphicon-share-alt"></i><a href="javascript:selectRoleById(${it.id});">选择角色</a>
										<a href="javascript:deleteById([${it.id}]);">删除</a>
										
									</td>
								</tr>
							</#list>
						<#else>
							<tr>
								<td class="text-center danger" colspan="6">没有找到用户</td>
							</tr>
						</#if>
					</table>
					
					</form>
				</div>
			</div><#--/row-->
		</div>
			<#--弹框-->
			<div class="modal fade bs-example-modal-sm"  id="selectRole" tabindex="-1" role="dialog" aria-labelledby="selectRoleLabel">
			  <div class="modal-dialog modal-sm" role="document">
			    <div class="modal-content">
			      <div class="modal-header">
			        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			        <h4 class="modal-title" id="selectRoleLabel">添加角色</h4>
			      </div>
			      <div class="modal-body">
			        <form id="boxRoleForm">
			          loading...
			        </form>
			      </div>
			      <div class="modal-footer">
			        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
			        <button type="button" onclick="selectRole();" class="btn btn-primary">Save</button>
			      </div>
			    </div>
			  </div>
			</div>
			
	</body>
</html>
<script>
so.init(function(){
				//初始化全选。
				so.checkBoxInit('#checkAll','[check=box]');
				
				
			
			});
function selectRole(){
				var checked = $("#boxRoleForm :checked");
				var ids=[],names=[];
				$.each(checked,function(){
					ids.push(this.id);
					names.push($.trim($(this).attr('name')));
				});
				var index = layer.confirm("确定操作？",function(){
					
					var load = layer.load();
					$.post('updateRoles.shtml',{checkedStr:ids.join(','),uid:$('#selectUserId').val()},function(result){
						layer.close(load);
						if(result && result.status != 200){
							return layer.msg(result.message,so.default1),!1;
						}
						layer.msg('添加成功。');
						setTimeout(function(){
							$('#formId').submit();
						},1000);
					},'json');
				});
			}
			
/**根据角色ID选择权限，分配权限操作。*/
function selectRoleById(id){
  var load = layer.load();
  $.post("/RBAC/user/selectAllRoles.shtml",{id:id},function(result){
                   
					layer.close(load);
					if(result && result.length){
						var html =[];
						$.each(result,function(){
							html.push("<div class='checkbox'><label>");
							html.push("<input type='checkbox' id='");
							html.push(this.id);
							html.push("'");
							if(this.checked==true){
								html.push(" checked='checked'");
							}
							html.push("value='");
							html.push(this.id);
							html.push("'");
							html.push("name='");
							html.push(this.name);
							html.push("'/>");
							html.push(this.name);
							html.push('</label></div>');
						});
						
						return so.id('boxRoleForm').html(html.join('')) & $('#selectRole').modal(),$('#selectUserId').val(id),!1;
					}else{
						return layer.msg("执行错误",so.default1);
					}
				},'json');
  
}

			
			
		
		</script>